﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tasks.Business;
using Tasks.Common.Entitys;
using System.Collections;
using System.Web;
using Tasks.Common.Enum;

namespace Tasks.Core
{
    /// <summary>
    /// 权限控制类
    /// </summary>
    public class Power
    {
        /// <summary>
        /// 用户权限
        /// </summary>
        public IList<UserPower> Powers
        {
            get
            {
                object powers = HttpContext.Current.Session["USER_POWER"];
                if (powers == null)
                {
                    if (Config.CurrentOperator == null)
                        return new List<UserPower>();
                    else
                    {
                        PowerBLL powerBLL = BusinessFactory.Power;
                        this.Powers = powerBLL.GetUserPowersByUserID(Config.CurrentOperator.UserID);
                        return this.Powers;
                    }
                }
                else
                {
                    return powers as List<UserPower>;
                }
            }
            set
            {
                HttpContext.Current.Session["USER_POWER"] = value;
            }
        }

        public IList<PWRole2Menu> ChildMenuPowers
        {
            get
            {
                Object menupowers = HttpContext.Current.Session["USERMENU_POWER"];
                if (menupowers == null)
                {
                    if (Config.CurrentOperator == null)
                        return new List<PWRole2Menu>();
                    else
                    {
                        PowerBLL powerBLL = BusinessFactory.Power;
                        this.ChildMenuPowers = powerBLL.GetMenuPowerByUserID(Config.CurrentOperator.UserID);
                        return this.ChildMenuPowers;
                    }
                }
                else
                {
                    return menupowers as List<PWRole2Menu>;
                }
            }
            set
            {
                HttpContext.Current.Session["USERMENU_POWER"] = value;
            }
        }
        /// <summary>
        /// 验证是否有菜单指定权限
        /// </summary>
        /// <param name="menuCode"></param>
        /// <returns></returns>
        public virtual bool ValidateChildMenu(int menuCode)
        {
            if (Config.CurrentOperator.UserID == Config.AdminID)
                return true;
            if (this.ChildMenuPowers == null)
                return false;
            foreach (PWRole2Menu m in this.ChildMenuPowers)
            {
                if (m.MenuID == menuCode)
                    return true;
            }
            return false;
        }
        public IList<PWRole2Menu> ParentMenuPowers
        {
            get
            {
                Object menupowers = HttpContext.Current.Session["USERPARENTMENU_POWER"];
                if (menupowers == null)
                {
                    if (Config.CurrentOperator == null)
                        return new List<PWRole2Menu>();
                    else
                    {
                        PowerBLL powerBLL = BusinessFactory.Power;
                        this.ParentMenuPowers = powerBLL.GetParentMenuPowerByUserID(Config.CurrentOperator.UserID);
                        return this.ParentMenuPowers;
                    }
                }
                else
                {
                    return menupowers as List<PWRole2Menu>;
                }
            }
            set
            {
                HttpContext.Current.Session["USERPARENTMENU_POWER"] = value;
            }
        }
        /// <summary>
        /// 验证父菜单是否有指定权限
        /// </summary>
        /// <param name="menuCode"></param>
        /// <returns></returns>
        public virtual bool ValidateParentMenu(int menuID)
        {
            if (Config.CurrentOperator.UserID == Config.AdminID)
                return true;
            if (this.ParentMenuPowers == null)
                return false;
            foreach (PWRole2Menu m in this.ParentMenuPowers)
            {
                if (m.MenuID == menuID)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 验证是否有指定动作权限
        /// </summary>
        /// <param name="url">访问路径</param>
        /// <returns></returns>
        public virtual bool ValidateUrl(string url)
        {
            if (Config.CurrentOperator.UserID == Config.AdminID)
                return true;

            if (this.Powers == null)
                return false;

            foreach (UserPower p in this.Powers)
            {
                if (!string.IsNullOrEmpty(p.Url) && p.Url.IndexOf(url) != -1)
                    return true;
            }

            return false;
        }

        /// <summary>
        /// 验证是否有指定动作权限
        /// </summary>
        /// <param name="moduleCode">模块代码</param>
        /// <param name="actionCode">动作代码</param>
        /// <returns></returns>
        public virtual bool ValidatePower(string moduleCode, string actionCode)
        {
            //admin 为超级管理员，具备所有权限
            if (Config.CurrentOperator.UserID == Config.AdminID)
                return true;

            if (this.Powers == null)
                return false;

            moduleCode = moduleCode.ToLower();
            foreach (UserPower ma in this.Powers)
            {
                if (string.IsNullOrEmpty(actionCode))
                {
                    if (ma.ModuleCode.ToLower() == moduleCode)
                        return true;
                }
                else
                {
                    if (ma.ModuleCode.ToLower() == moduleCode && ma.ActionCode.ToLower() == actionCode.ToLower())
                        return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 验证是否具有模块权限
        /// </summary>
        /// <param name="moduleCode">模块代码</param>
        /// <returns></returns>
        public bool ValidatePower(string moduleCode)
        {
            return this.ValidatePower(moduleCode, "");
        }

    }
}
